<?php

    class InventoryWriterController extends Controller
    {

        public $layout = '//layouts/column2';
        protected $writeHandle = null;

        public function actionIndex()
        {

            $file = CUploadedFile::getInstanceByName('file');
            if ($file != null)
            {
                if ($file->extensionName == 'csv')
                {
                    $name = 'documents/' . 'temp_' . date('Y-m-d-H-i-s') . $file->extensionName;

                    if ($file->saveAs($name, true))
                    {
                        $f = fopen($name, 'r');
                        if ($f !== false)
                        {
                            $outputCsvName = 'csv/inventory-ouput-' . date('Y-m-d-H-i-s') . '.csv';
                            $this->writeHandle = fopen($outputCsvName, 'w+');
                            $this->connectToDb();
                            while ($lineArrray = fgetcsv($f, 1024, ','))
                            {
                                if (count($lineArrray) == 2)
                                {
                                    $data = $this->getPartData($lineArrray[0]);
                                    if ($data != false)
                                    {
                                        $data['qty'] = $lineArrray[1];
                                        $this->writeToFile($data);
                                    }
                                }
                            }
                            fclose($this->writeHandle);
                            fclose($f);
                            Yii::app()->fileHelper->forceDownload($outputCsvName);
                            unlink($outputCsvName);
                            unlink($name);
                            Yii::app()->end();
                        }
                    }
                }
            }
            $this->render('index');
        }

        protected function writeToFile($data)
        {
            $line = $data['partno'] . ',' . $data['desc'] . ',' . $data['lastprice'] . ',' . $data['qty'] . PHP_EOL;
            fwrite($this->writeHandle, $line);
        }

        protected $dbhandle;
        protected $db;

        protected function connectToDb()
        {
            $myServer = "primary.qsibanking.com:1189";
            $myUser = "CustomerConnect";
            $myPass = "letmein";
            $myDB = "qsilive";
            $this->dbhandle = mssql_connect($myServer, $myUser, $myPass);
            $this->db = mssql_select_db($myDB, $this->dbhandle);
        }

        protected function getPartData($partno)
        {

//            static $command;
//            if (!$command)
//            {
//                $command = Yii::app()->dbqsilive->createCommand('select * from inven where partno = :partno');
//            }
//            $row = $command->bindValue('partno', $partno)->queryRow();
            $query = mssql_query('select * from inven where Part="' . $partno . '"');
            $row = mssql_fetch_array($query);
            $result = false;
            if ($row !== false)
            {
                $result = array(
                    'partno' => $partno,
                    'desc' => stripExtraSpace(trim(str_replace(array(',','\r','\n'),'',$row['Desc']))),
                    'lastprice' => $row['AverageC']
                );
            }else{
                $result = array(
                    'partno' => $partno,
                    'desc' => 'Not in ESC',
                    'lastprice' => '0.00'
                );
            }
            
            return $result;
        }

    }

    